package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.*;
import com.sky.entity.Dish;
import com.sky.entity.Employee;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.EmployeeMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class SetMealServiceImpl implements SetMealService {
     @Autowired
     private SetMealMapper setMealMapper;
     @Autowired
     private SetMealDishMapper setMealDishMapper;
     @Autowired
     private DishMapper dishMapper;
    //新增套餐
    @Override
    @Transactional
    public void saveWithDishs(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入一条数据
        setMealMapper.insert(setmeal);
        Long setmealId=setmeal.getId();
        //向套餐菜品关系表插入n条数据
        List<SetmealDish> setmealDishList=setmealDTO.getSetmealDishes();
        if (setmealDishList!=null&&setmealDishList.size()>0){
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
        }
        setMealDishMapper.insertBatch(setmealDishList);
    }
    //套餐分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setMealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //套餐批量删除
    @Override
    public void del(List<Long> ids) {
        // 判断当前套餐是否能够删除---是否存在起售中的套餐？？
        for (Long id:ids) {
            Setmeal setmeal = setMealDishMapper.chaxunById(id);
            if (Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 删除套餐表中的套餐数据
        for (Long id:ids){
            setMealMapper.deleteById(id);

            // 删除套餐关联的菜品数据
            setMealDishMapper.deleteBySetmealId(id);
    }
}

    @Override
    //根据套餐表主键id查询套餐
    public SetmealVO list(Long id) {
        Setmeal setmeal=setMealMapper.list(id);
        // 根据套餐id查询关联菜品
        List<SetmealDish> setmealDishList=setMealDishMapper.chaxuncaipinById(id);
        // 将查询到的数据封装到VO
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    @Override
    //修改套餐
    public void update(SetmealDTO setmealDTO) {
        //修改套餐信息
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.update(setmeal);
        //删除套餐相关联的菜品
        setMealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //重新插入套餐相关联的菜品
        List<SetmealDish> setmealDishList=setmealDTO.getSetmealDishes();
        if (setmealDishList != null && setmealDishList.size() > 0) {
            setmealDishList.forEach(sd -> {
                sd.setSetmealId(setmealDTO.getId());
            });
            // 向套餐菜品关系表插入n条数据
            setMealDishMapper.insertBatch(setmealDishList);

    }
}

    @Override
    //启用禁用套餐
    public void stopOrStart(Integer status, Long id) {
        if (status==StatusConstant.ENABLE){
            //看关联菜品中有没有未启售的
            List<SetmealDish> setmealDishList=setMealDishMapper.chaxuncaipinById(id);
            setmealDishList.forEach(setmealDish -> {
                Dish dish=dishMapper.chaxunById(setmealDish.getDishId());
                if (dish.getStatus()==StatusConstant.DISABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });}
        Setmeal setmeal=setMealMapper.list(id);
        setmeal.setStatus(status);
        setMealMapper.update(setmeal);

    }

    @Override
    //根据分类id查询套餐
    public List<Setmeal> listByCategoryId(Long categoryId) {
        return setMealMapper.listByCategoryId(categoryId);
    }

    @Override
    //根据套餐id查询包含的菜品
    public List<SetmealDish> listDishBySetmealId(Long setmealId) {
        return setMealMapper.listDishBySetmealId(setmealId);
    }
}
